/**
 * Created by bitfeng on 16/4/26.
 */
var webPage = require('webpage');
var page = webPage.create();

var url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=%E6%B5%8B%E8%AF%95&rsv_pq=c8d5bf2400007b78&rsv_t=733ek%2FDSYqu5y0NRJGiJkrD5ge2AgpFLLn8FQ%2B7lDt3XRHq%2Fsu1JNZcyNm8&rsv_enter=1&rsv_sug3=7&rsv_sug1=4&rsv_sug7=100&rsv_sug2=0&inputT=719&rsv_sug4=8720";
console.log(url);

// 打印页面标题
function print_title(page){
    var title = page.evaluate(function(){
        return document.title;
    });
    console.log(title);
}

// 获取当前页面
function get_current_page(page){
    return document.getElementById('page').getElementsByTagName('strong')[0].getElementsByClassName('pc')[0].textContent;
}

// click动作, 使用evaluate
function pagedown(page){
    console.log('start PageDown');
    page.evaluate(function(){
        document.getElementsByClassName('n')[0].click();
    });
    console.log('finish PageDown');
}

// click动作, 使用evaluateJavaScript
function pagedown_script(page){
    console.log('start PageDown');
    var script = "function(){document.getElementsByClassName('n')[0].click();}";
    page.evaluateJavaScript(script);
    console.log('finish PageDown');
}

// click动作, 直接获取第Ｎ页
function pagedown_script(page){
    console.log('start PageDown');
    var script = "function(){document.getElementsByClassName('n')[0].click();}";
    page.evaluateJavaScript(script);
    console.log('finish PageDown');
}


page.open(url, function(status) {
    console.log('HTTP Status: '+status);
    print_title(page);

    // console.log('Page_current: '+get_current_page(page));
    pagedown(page);
    // pagedown_script(page);
    console.log(page.content);
    // console.log('Page_current: '+get_current_page(page));
    //
    // pagedown_script(page);
    // console.log('Page_current: '+get_current_page(page));

    phantom.exit();
});